package com.abss.vote.controller;

import com.abss.vote.model.*;
import com.abss.vote.service.UserBackstageService;
import com.abss.vote.service.UserReceptionService;
import com.abss.vote.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.OutputStream;
import java.util.List;

@Controller
public class ExcelExport {


    @Autowired
    private UserBackstageService userBackstageService;

    @Autowired
    private UserReceptionService userReceptionService;

    @Autowired
    private MyProps myProps;



    //导出
    @RequestMapping(value = "/export")
    @ResponseBody
    public void exportFile(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        Administrators ad =(Administrators)session.getAttribute("ADMIN");
        //获取是否是超级管理员
        String isSup = ad.getIS_SUPER_ADMIN();
        //获取地址ID
        int districtId=ad.getDISTRICT_ID();
        int ACTIVITY_ID=ad.getLAST_ACTIVITY_ID();
        String id = request.getParameter("id");
        //获取数据
        List<UserWorkVo> list = userBackstageService.getList(ACTIVITY_ID,districtId,isSup,id);


        // 生成Excel
        //excel标题
        String[] title = {"作品编号","作品封面","作品名称","作者","年龄","联系方式","推荐单位","审核状态","作品类型","获得票数","浏览次数","奖项信息","上传时间","作品描述"};
        //excel名称
        String fileName = "作品信息统计表.xls";
        //sheet名
        String sheetName = "作品信息表";

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbooks(sheetName, title, list,null,mp);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
        }
    }


    //导出
    @RequestMapping(value = "/exportAllSignInfo")
    @ResponseBody
    public void exportAllSignInfo(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        Administrators ad = (Administrators) session.getAttribute("ADMIN");
        List<DistrictListUserWorkVo> list = userBackstageService.getAllUserRegInfo(null, ad);
        int activityId = ad.getLAST_ACTIVITY_ID();
        Activity activity = userBackstageService.getActivityById(activityId);

        // 生成Excel
        //excel标题
        String[] title = { "微信号", "姓名", "性别", "出生日期", "年龄", "推荐地区", "推荐单位", "联系电话", "报名时间" };
        //excel名称
        String fileName = activity.getACTIVITY_NAME() + "报名信息表.xls";
        //sheet名
        String sheetName = "报名信息";

        HSSFWorkbook wb = ExcelUtil.getSignInfoHSSFWorkbook(sheetName, title, list, null, mp);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
        }
    }
    
    @RequestMapping(value = "/exportAllWorkDetail")
    @ResponseBody
    public void exportAllWorkDetail(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        Administrators ad = (Administrators) session.getAttribute("ADMIN");
        List<DistrictListUserWorkVo> list = userBackstageService.getAllUserRegInfo(null, ad);
        int activityId = ad.getLAST_ACTIVITY_ID();
        Activity activity = userBackstageService.getActivityById(activityId);

        // 生成Excel
        // excel标题
        String[] title = { "微信号", "姓名", "性别", "出生日期", "年龄", "推荐地区", "推荐单位", "联系电话", "报名时间", "测评次数", "测评等级", "测评得分",
                "答题用时(秒)", "当前状态" };
        // excel名称
        String fileName = activity.getACTIVITY_NAME() + "活动统计信息表.xls";
        // sheet名
        String sheetName = "活动统计信息";

        HSSFWorkbook wb = ExcelUtil.getWorkDeatailHSSFWorkbook(sheetName, title, list, null, mp);
        // 响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getMessage());
        }
    }

    /**
     * 导出单个报表
     * @return
     */
    @RequestMapping(value = "/exportOne")
    @ResponseBody
    public void exportOne(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception {
        MpInfo mp = userReceptionService.getMpInFo(myProps.getServerid());
        String ID= request.getParameter("ID");
        //获取数据
        List<UserWorkVo> list = userBackstageService.getListOne(ID);

        // 生成Excel
        //excel标题
        String[] title = {"作品编号","作品封面","作品名称","作者","年龄","联系方式","推荐单位","审核状态","作品类型","获得票数","奖项信息","上传时间","作品描述"};
        //excel名称
        String fileName = "作品信息统计表.xls";
        //sheet名
        String sheetName = "作品信息表";

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbooks(sheetName, title, list,null,mp);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    private void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (Exception e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }















































}
